package com.vietthanh85.android;

import java.util.Date;
import java.util.Map.Entry;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AddWords extends Activity {
	
	private String spinnerArray[] = new String[7];
	EditText wordET;
	EditText meaningET;
	Spinner spinner;
	String type;
	String liteType;
	Button updateBtn;	
	
	String word;
	int typeOrder;
	String meaning;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addwords);
		
		Common.typeMap.values().toArray(spinnerArray);
		spinner = (Spinner) findViewById(R.id.typeSpinner);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spinnerArray);
		spinner.setAdapter(adapter);
		wordET = (EditText) findViewById(R.id.wordET);
		meaningET = (EditText) findViewById(R.id.meaningET);
		updateBtn = (Button) findViewById(R.id.updateBtn);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			word = extras.getString("word");
			typeOrder = extras.getInt("typeOrder");
			meaning = extras.getString("meaning");			
			wordET.setText(word);
			spinner.setSelection(typeOrder);
			meaningET.setText(meaning);
		}

		updateBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {				
				type = spinner.getSelectedItem().toString();
				for (Entry<String, String> entry : Common.typeMap.entrySet()) {
					if (entry.getValue().equals(type)) {
						liteType = entry.getKey();
					}
				}				
				if (wordET.getText().toString().trim().equals("")) {
					Toast.makeText(AddWords.this, "Please fill the word!",
							Toast.LENGTH_LONG).show();
					return;
				} else {
					if (liteType.equals("<later>")) {
						meaningET.setText("<later>");
					} else if (meaningET.getText().toString().trim().equals("")) {
						Toast.makeText(AddWords.this, "Please fill the word!",
								Toast.LENGTH_LONG).show();
						return;
					}
				}
				String curentDateandTime = Common.sdf.format(new Date());
				Line lineIns = new Line();				
				lineIns.word = wordET.getText().toString();				
				lineIns.type = liteType;
				lineIns.meaning = meaningET.getText().toString();
				lineIns.client = Common.client;
				lineIns.time = curentDateandTime;
				int loc = Common.word2Loc(Common.lineVector, lineIns.word);
				if (loc == -1) {
					lineIns.id = Common.lineVector.size() + 1;
					Common.lineVector.add(lineIns);
				} else {
					lineIns.id = loc + 1;
					Common.lineVector.remove(loc);
					Common.lineVector.insertElementAt(lineIns, loc);
				}
				Toast.makeText(AddWords.this, "Updated Successfully!",
						Toast.LENGTH_LONG).show();				
				wordET.setText("");
				meaningET.setText("");
				Common.changed = true;
			}
		});
	}
	
	public void finish() {
		if (Common.changed) {
			Search.handler.sendEmptyMessage(1);			
		}
		super.finish();
	}
}
